﻿using MaximServices.Common;
using MaximServices.Model;
using MaximServices.Repository;
using System;
using System.Collections.Generic;
using System.Text;

namespace MaximServices.Services
{
    public class MottoRuleServices
    {
        private MottoRuleRepository _mottoRuleRepository;

        private DateDetailRepository _dateDetailRepository;

        public MottoRuleServices(MottoRuleRepository mottoRuleRepository, DateDetailRepository dateDetailRepository)
        {
            _mottoRuleRepository = mottoRuleRepository;
            _dateDetailRepository = dateDetailRepository;
        }

        public PageData<MottoRule> GetList(MottoRule model, int pageIndex, int pageSize)
        {
            return _mottoRuleRepository.GetList(model, pageIndex, pageSize);
        }
        /// <summary>
        /// 添加规则
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResultModel<CreateMottoRuleResultModel> CreateMottoRule(MottoRule model)
        {
            var conflict = _mottoRuleRepository.GetModelByName(model.Rule_Name);
            if(null != conflict && 0 < conflict.Count)
            {
                return BuildResult.Error("规则名称已经存在了", new CreateMottoRuleResultModel
                {
                    CreateSelf = model,
                    Conflict = conflict
                });
            }
            model.Id = IdFactory.NextId;
            DateTime start = model.StartDate.Value;
            DateTime end = model.EndDate.Value;
            List<DateDetail> details = new List<DateDetail>();
            List<string> dates = new List<string>();
            for (DateTime i = start; i <= end; i = i.AddDays(1))
            {
                dates.Add($"'{i.ToString("yyyy-MM-dd")}'");
                details.Add(new DateDetail
                {
                    Id = IdFactory.NextId,
                    Target_Date = i,
                    Rule_Id = model.Id
                });
            }
            List<DateDetail> conflictDetail = _dateDetailRepository.GetList(dates);
            //禁止有重复
            if (null != conflictDetail && 0 < conflictDetail.Count)
            {
                List<string> ids = new List<string>();
                conflictDetail.ForEach(c => ids.Add(c.Rule_Id));
                var ruleList = _mottoRuleRepository.GetList(ids);
                return BuildResult.Error("日期有冲突", new CreateMottoRuleResultModel
                {
                    CreateSelf = model,
                    Conflict = ruleList
                });
            }
            
            if(_mottoRuleRepository.CreateMottoRule(model, details))
            {
                return BuildResult.Success(new CreateMottoRuleResultModel
                {
                    CreateSelf = model,
                    Conflict = null
                });
            }
            else
            {
                return BuildResult.Error("添加出错了",new CreateMottoRuleResultModel
                {
                    CreateSelf = model,
                    Conflict = null
                });
            }
        }
        /// <summary>
        /// 修改规则
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResultModel<CreateMottoRuleResultModel> ModifyMottoRule(MottoRule model)
        {
            if(_mottoRuleRepository.ModifyMottoRule(model))
            {
                return BuildResult.Success(new CreateMottoRuleResultModel
                {
                    CreateSelf = model
                });
            }
            else
            {
                return BuildResult.Error("修改出错了", new CreateMottoRuleResultModel
                {
                    CreateSelf = model,
                    Conflict = null
                });
            }
        }

        public ResultModel<CreateMottoRuleResultModel> DeleteMottoRule(long Id)
        {
            if (_mottoRuleRepository.DeleteMottoRule(Id))
            {
                return BuildResult.Success(new CreateMottoRuleResultModel
                {
                    CreateSelf = null
                });
            }
            else
            {
                return BuildResult.Error("修改出错了", new CreateMottoRuleResultModel
                {
                    CreateSelf = null,
                    Conflict = null
                });
            }
        }
    }
}
